<template>
  <t-tabs v-model:value="engineeringDetails.selectedTab" :list="list" />
</template>

<script setup lang="ts">
import { MessagePlugin } from 'tdesign-vue-next';
import { computed, inject, Ref, ref, watch } from 'vue';

import { useDictionaryStore } from '@/store';

// 字典仓库
const dictionaryStore = useDictionaryStore();
// 订单详情
const engineeringDetails = inject<Ref<Record<string, any>>>('engineeringDetails');
// 列表
const list = ref([
  { label: '施工节点', value: '施工节点' },
  { label: '施工图片', value: '施工图片' },
  // { label: '施工巡检', value: '施工巡检' },
  { label: '材料进度', value: '材料进度' },
  // { label: '材料汇总', value: '材料汇总' },
  // { label: '人工费用', value: '人工费用' },
  { label: '洽商变更', value: '洽商变更' },
  { label: '验收单', value: '验收单' },
  { label: '回访记录', value: '回访记录' },
  { label: '设计信息', value: '设计信息' },
  // { label: '初步预算', value: '初步预算' },
  { label: '合同信息', value: '合同信息' },
  { label: '产品进度', value: '产品进度' },
  // { label: '施工视频', value: '施工视频' },
  { label: '施工结算', value: '施工结算' },
  { label: '延期单', value: '延期单' },
])
// 设置选中的标签
watch(
  list,
  () => {
    let selectedTab = list.value[0]?.value;
    const { operateType, approveType } = engineeringDetails.value;
    if (operateType) {
      let operateTypeConfig = { tab: '' };
      try {
        const operateTypeDictionary: Record<string, any> =
          dictionaryStore.get('decoration_order_operate_type').find((item) => item.value === operateType) || {};
        operateTypeConfig = JSON.parse(operateTypeDictionary.remark);
        if (operateTypeConfig.tab) selectedTab = operateTypeConfig.tab;
      } catch {
        MessagePlugin.warning('解析操作配置失败，请联系管理员。');
      }
    } else if (approveType) {
      let approveTypeConfig = { tab: '' };
      try {
        const approveTypeDictionary: Record<string, any> =
          dictionaryStore.get('order_approval_type').find((item) => item.value === approveType) || {};
        approveTypeConfig = JSON.parse(approveTypeDictionary.remark);
        if (approveTypeConfig.tab) selectedTab = approveTypeConfig.tab;
      } catch {
        MessagePlugin.warning('解析审批配置失败，请联系管理员。');
      }
    }
    engineeringDetails.value.selectedTab = selectedTab;
  },
  { immediate: true },
);
</script>
